/*
 * @Descripttion:
 * @Author: voanit
 * @Date: 2022-08-26 09:09:05
 * @LastEditors: voanit
 * @LastEditTime: 2022-08-27 15:20:22
 */
import Vue from 'vue'
import Vuex from 'vuex'
import user from './modules/user'
Vue.use(Vuex)
const store = new Vuex.Store({
  state: {
    cartList: uni.getStorageSync('cartList') || [
      {
        // 商品id
        goods_id: '111',
        // 商品图片
        goods_small_logo:
          'http://image3.suning.cn/uimg/b2c/newcatentries/0070075424-000000000137468134_1_400x400.jpg',
        // 商品名称
        goods_name:
          '朗龙(Rhone)净味炭膏 新车除甲醛汽车除味剂 车载空气净化 车内去异味 RC11K',
        // 商品价格
        goods_price: 118,
        // 商品数量
        goods_count: 1,
        // 商品选中状态
        goods_select: true,
      },
      {
        goods_id: '2222',
        goods_small_logo:
          'http://image2.suning.cn/uimg/b2c/newcatentries/0070134451-000000000162856655_2_400x400.jpg',
        goods_name:
          '捷安特自行车城市休闲车女式22寸24寸莫曼顿莉莉复古淑女车学生车代步通勤车',
        goods_price: 688,
        goods_count: 2,
        goods_select: false,
      },
    ],
  },
  mutations: {
    setCart(state, cartList) {
      console.log(1)
      state.cartList = cartList
      //持久化
      uni.setStorageSync('cartList', cartList)
    },
  },
  actions: {},
  getters: {
    isAll(state) {
      console.log(12334)
      // 全选
      return state.cartList.every((item) => item.goods_select)
    },
    total(state) {
      /* 计算总价钱
      1-筛选出选中的商品
      2-根据选中的商品计算总价钱
      */
      return state.cartList
        .filter((item) => item.goods_select)
        .reduce((sum, item) => sum + item.goods_price * item.goods_count, 0)
    },
    totalCount(state) {
      return state.cartList
        .filter((item) => item.goods_select)
        .reduce((sum, item) => sum + item.goods_count, 0)
    },

    /* 获取选中的商品集合 */
    selectCartList(state) {
      return state.cartList.filter((item) => item.goods_select)
    },
  },
  modules: {
    user,
  },
})

export default store
